/*********************************************************************************
 *      Copyright:  (C) 2022 YanlinZhou<1222>
 *                  All rights reserved.
 *
 *       Filename:  msqt_client.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/18/2022)
 *         Author:  YanlinZhou <1222>
 *      ChangeLog:  1, Release initial version on "04/18/2022 08:20:38 PM"
 *                 
 ********************************************************************************/

#include "json.h"

#define ALI 1
#define TENGXUN 2
#define HUAWEI 3

int pack_in_json(pack_ctx_t *json, time_ctx_t *t, mqtt_ctx_t mqtt_ctx) 
{
	    /* 创建一个JSON数据对象(链表头结点) */
   		json->cjson_head = cJSON_CreateObject();
		json->item = cJSON_CreateObject();
		
		if(ALI == mqtt_ctx.platform)
		{
			/* 添加一条浮点类型的JSON数据(添加一个链表节点) */
			cJSON_AddItemToObject(json->cjson_head, "method", cJSON_CreateString(json->method));//根节点下添加
		    cJSON_AddItemToObject(json->cjson_head, "id", cJSON_CreateString(json->id));//根节点下添加
   			cJSON_AddItemToObject(json->cjson_head, "params", json->item);
 		    cJSON_AddItemToObject(json->item, "CurrentTemperature", cJSON_CreateNumber(json->payload));
 		    cJSON_AddItemToObject(json->item, "CurrentTime", cJSON_CreateString(t->times));
		    cJSON_AddItemToObject(json->cjson_head, "version", cJSON_CreateString(json->version));

			/* 打印JSON对象(整条链表)的所有数据 */
   		 	json->str = cJSON_Print(json->cjson_head);
 //  		 	printf("str = :\n%s", json->str);
			return 0;
		}

		else if(TENGXUN == mqtt_ctx.platform)
		{
			/* 添加一条浮点类型的JSON数据(添加一个链表节点) */
			cJSON_AddItemToObject(json->cjson_head, "method", cJSON_CreateString(json->method));//根节点下添加
		    cJSON_AddItemToObject(json->cjson_head, "clientToken", cJSON_CreateString(json->id));//根节点下添加
   			cJSON_AddItemToObject(json->cjson_head, "params", json->item);
 		    cJSON_AddItemToObject(json->item, "temperature", cJSON_CreateNumber(json->payload));
 		    cJSON_AddItemToObject(json->item, "time", cJSON_CreateString(t->times));

			/* 打印JSON对象(整条链表)的所有数据 */
   		 	json->str = cJSON_Print(json->cjson_head);
//   		 	printf("str = :\n %s", json->str);
			return 0;
		}

		else if(HUAWEI == mqtt_ctx.platform)
		{
			cJSON       *item2;
			cJSON       *item3;
   			json->cjson_head = cJSON_CreateObject();
			json->item = cJSON_CreateArray();
   			item2 = cJSON_CreateObject();
   			item3 = cJSON_CreateObject();

			/* 添加一条浮点类型的JSON数据(添加一个链表节点) */
			cJSON_AddItemToObject(json->cjson_head, "services", json->item);
			cJSON_AddItemToObject(json->item, "services", item2);
 		    cJSON_AddItemToObject(item2, "service_id", cJSON_CreateString(json->id));
			cJSON_AddItemToObject(item2, "properties", item3);
 		    cJSON_AddItemToObject(item3, "temperature", cJSON_CreateNumber(json->payload));
 		    cJSON_AddItemToObject(item3, "time", cJSON_CreateString(t->times));

			/* 打印JSON对象(整条链表)的所有数据 */
   		 	json->str = cJSON_Print(json->cjson_head);
//  			printf("str = :\n%s", json->str);
			return 0;
		}
}

